package com.xjh.basestudy.lanqiaobei.base;

import java.util.Arrays;

public class Arrays_ {
    public static void main(String[] args) {
        // 1. 创建和初始化数组
        int[] numbers = {5, 2, 9, 1, 5, 6};
        System.out.println("原始数组：" + Arrays.toString(numbers));
//        创建一个不指定元素的数组（但是数组必须指定大小）
        int[] arr = new int[10];
        // 2. 遍历数组
        System.out.print("遍历数组（普通 for 循环）：");
        for (int i = 0; i < numbers.length; i++) {
            System.out.print(numbers[i] + " ");
        }
        System.out.println();

        System.out.print("遍历数组（增强型 for 循环）：");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
        System.out.println("\n");

        // 3. 数组排序
        Arrays.sort(numbers);
        System.out.println("排序后的数组：" + Arrays.toString(numbers));

        // 4. 数组查找
        int target = 5;
        int index = Arrays.binarySearch(numbers, target);
        if (index >= 0) {
            System.out.println("二分查找：目标值 " + target + " 的索引是 " + index);
        } else {
            System.out.println("二分查找：目标值 " + target + " 不在数组中");
        }

        // 5. 数组复制
        int[] copy1 = Arrays.copyOf(numbers, numbers.length);
        System.out.println("复制后的数组（Arrays.copyOf）：" + Arrays.toString(copy1));

        int[] copy2 = new int[numbers.length];
        System.arraycopy(numbers, 0, copy2, 0, numbers.length);
        System.out.println("复制后的数组（System.arraycopy）：" + Arrays.toString(copy2));

        // 6. 数组填充
        int[] filledArray = new int[5];
        Arrays.fill(filledArray, 10);
        System.out.println("填充后的数组：" + Arrays.toString(filledArray));

        // 7. 数组比较
        boolean isEqual = Arrays.equals(numbers, copy1);
        System.out.println("比较数组是否相等：" + isEqual);

        // 修改 copy1 的一个元素
        copy1[0] = 100;
        System.out.println("修改后 copy1：" + Arrays.toString(copy1));
        System.out.println("再次比较数组是否相等：" + Arrays.equals(numbers, copy1));


    }
}
